package com.xci.edu.module.foreground.pc.train.controller;

import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.XCI;
import com.xci.edu.core.entity.ExerciseQuestion;
import com.xci.edu.core.service.EduQuestionFavoriteService;
import com.xci.edu.core.service.EduQuestionService;
import com.xci.edu.core.service.TrainStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/train/simulatorExercise")
public class SimulatorExerciseController {
    /** 试题库服务 */
    @Resource private EduQuestionService eduQuestionService;
    /** 学员培训登录 */
    @Resource private TrainStudentService trainStudentService;
    /** 题库收藏服务 */
    @Resource private EduQuestionFavoriteService eduQuestionFavoriteService;

    /** 试题集*/
    private List<ExerciseQuestion> questionList;
    /** 出题类型 1--顺序联系，2--随即练习*/
    private Integer type;
    /** 学员Id*/
    private Long studentId;

    /** 模拟练习页面 */
    @GetMapping("/index")
    public String index() {
        //如果为空，则查询题库，并设置出题类型为顺序出题
        if(XCI.isBlank(questionList)){
            Long id = trainStudentService.getCurrentStudent().getEduCategoryId();
            questionList = eduQuestionService.getExerciseQuestions(id,null);
            type=1;
            studentId = trainStudentService.getCurrentStudent().getId();
        }
        return "foreground/pc/train/eduSimulatorExercise/index";
    }

    /** 获取练习题 */
    @PostMapping("/getQuestion")
    public String getQuestion(@RequestParam(defaultValue = "0" ) Integer index, ModelMap map){
        int count = questionList.size();
        ExerciseQuestion entity = null;
        if (index >= 0 && index < count) entity = questionList.get(index);

        boolean isFavorite = eduQuestionFavoriteService.selectByIdCondition(studentId,entity.getId(),2);
        map.put("entity", entity);
        map.put("index", index);
        map.put("size", count);
        map.put("type",type);
        map.put("isFavorite",isFavorite);
        long errorCount = questionList.stream().filter(q->q.getUserSelected()!=null && q.getUserResult()).count();
        long correctCount = questionList.stream().filter(q->q.getUserSelected()!=null && !q.getUserResult()).count();
        map.put("errorCount",errorCount);
        map.put("correctCount",correctCount);
        return "foreground/pc/train/eduSimulatorExercise/question";
    }

    /** 记录答题情况 */
    @ResponseBody
    @PostMapping("/checkQuestion")
    public BoolResult checkQuestion(Long questionId, Boolean userResult, String userAnswer){
        questionList.stream().filter(
                item->item.getId().equals(questionId)
        ).findFirst().ifPresent(q->{
            q.setUserSelected(true);
            q.setUserResult(userResult);
            q.setUserAnswer(userAnswer);
        });
        if (!userResult){
            eduQuestionFavoriteService.insertError(studentId, questionId);
        }
        return BoolResult.success();
    }

    /** 收藏题库 */
    @ResponseBody
    @PostMapping("/favoriteQuestion")
    public BoolResult favoriteQuestion(Long questionId, Integer favoriteType, Boolean isFavorite){
        return isFavorite ?
                eduQuestionFavoriteService.insert(studentId, questionId, favoriteType) :
                eduQuestionFavoriteService.delete(studentId, questionId, favoriteType);
    }

    /** 顺序获取练习题 */
    @ResponseBody
    @PostMapping("/seq")
    public BoolResult seq(){
        type=1;
        Long id = trainStudentService.getCurrentStudent().getEduCategoryId();
        questionList = eduQuestionService.getExerciseQuestions(id,null);
        return BoolResult.success();
    }

    /** 随机获取练习题 */
    @ResponseBody
    @PostMapping("/random")
    public BoolResult random(){
        type=2;
        Long id = trainStudentService.getCurrentStudent().getEduCategoryId();
        questionList = eduQuestionService.getExerciseQuestions(id,null);
        Collections.shuffle(questionList);
        return BoolResult.success();
    }


}
